package com.jdkreader.concurrency.queue;

public class Eater implements Runnable{

    private ToastQueue finishedQueue;

    private int count = 0;

    public Eater(ToastQueue finished){
        this.finishedQueue = finished;
    }

    @Override
    public void run() {

        try{
            while(!Thread.interrupted()){
                Toast t = finishedQueue.take();
                if(t.getId() != count++ ||
                t.getStatus() != Toast.Status.JAMMED){
                    System.out.println(">>> ERROR: "+t);
                    System.exit(1);
                }else{
                    System.out.println("eat "+t);
                }
            }
        }catch(InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("Eater off");
    }
}
